package com.ruoyi.web.controller;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.entity.Coupon;
import com.ruoyi.web.service.ICouponService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lqc
 * @since 2024-01-27
 */
@RestController
@RequestMapping("/admin/coupon")
public class CouponController extends BaseController {

    private final ICouponService couponService;

    public CouponController(ICouponService couponServiceImpl) {
        this.couponService = couponServiceImpl;
    }

    @PreAuthorize("@ss.hasPermi('admin:coupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(Coupon coupon) {
        startPage();
        List<Coupon> list = couponService.selectCouponList(coupon);
        return getDataTable(list);
    }

    @GetMapping("/getDatas")
    public AjaxResult getCouponDatas() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<Coupon> temp = couponService.list();
        for (Coupon coupon : temp) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("couponId", coupon.getId().toString());
            map.put("couponName", coupon.getCouponName());
            list.add(map);
        }
        return success(list);
    }

    /**
     * 导出优惠券管理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:coupon:export')")
    @Log(title = "优惠券管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Coupon coupon) {
        List<Coupon> list = couponService.selectCouponList(coupon);
        ExcelUtil<Coupon> util = new ExcelUtil<Coupon>(Coupon.class);
        util.exportExcel(response, list, "优惠券管理数据");
    }

    /**
     * 获取优惠券管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:coupon:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(couponService.selectCouponById(id));
    }

    /**
     * 新增优惠券管理
     */
    @PreAuthorize("@ss.hasPermi('admin:coupon:add')")
    @Log(title = "优惠券管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Coupon coupon) {
        coupon.setId(IdUtil.getSnowflakeNextId());
        return toAjax(couponService.insertCoupon(coupon));
    }

    @GetMapping("/getByUserId")
    public AjaxResult getByUserId(){
        List<Coupon> coupons = couponService.selectByCustomerId();
        return success(coupons);
    }

    /**
     * 修改优惠券管理
     */
    @PreAuthorize("@ss.hasPermi('admin:coupon:edit')")
    @Log(title = "优惠券管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Coupon coupon) {
        return toAjax(couponService.updateCoupon(coupon));
    }

    /**
     * 删除优惠券管理
     */
    @PreAuthorize("@ss.hasPermi('admin:coupon:remove')")
    @Log(title = "优惠券管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(couponService.deleteCouponByIds(ids));
    }
}
